package com.ujiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujiuye.config.PageResult;
import com.ujiuye.config.QueryPageBean;
import com.ujiuye.mapper.MemberMapper;
import com.ujiuye.pojo.Member;
import com.ujiuye.service.IMemberService;
import com.ujiuye.vo.MemberVo;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {
    @Resource
    private MemberMapper memberMapper;

    @Override
    public PageResult listPage(QueryPageBean queryPageBean) {
        Page<Member> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        QueryWrapper<Member> queryWrapper = new QueryWrapper();
        if (queryString!=null && queryString.length()>0){
            queryWrapper.eq("name",queryString).or().eq("phoneNumber",queryString);
        }
        if (!queryPageBean.getSex().equals("不限")){
            queryWrapper.eq("sex",queryPageBean.getSex());
        }
        if (!queryPageBean.getReg().equals("不限")){
            String startTime = queryPageBean.getReg()+"-01-01";
            String endTime = queryPageBean.getReg()+"-12-30";
            queryWrapper.between("regTime",startTime,endTime);
        }

        if (!queryPageBean.getAge().equals("不限")){
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH)+1;
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            if (queryPageBean.getAge().contains("-")) {
                String[] split = queryPageBean.getAge().split("-");//对年龄进行分割返回一个数组
                String begTime = (year - Integer.parseInt(split[0])) + "-" + month + "-" + day;
                String endTime = (year - Integer.parseInt(split[1])) + "-" + month + "-" + day;
                queryWrapper.between("birthday",endTime,begTime);
            }else {
                queryWrapper.lt("birthday",(year-60)+"-"+month+"-"+day);
            }
        }
        Page<Member> memberPage = memberMapper.selectPage(page, queryWrapper);
        PageResult pageResult = new PageResult(memberPage.getTotal(),memberPage.getRecords());


        return pageResult;
    }

    @Override
    public List<Member> list(int  currentPage, int  pageSize) {
        Page<Member> page  =new Page<>(currentPage,pageSize);
        QueryWrapper<Member> queryWrapper= new QueryWrapper();
        Page<Member> memberPage = memberMapper.selectPage(page, queryWrapper);
        List<Member> memberList = memberPage.getRecords();
        return memberList;
    }

    @Override
    public MemberVo memberList() {
        List<String> xAxis = new ArrayList<>();
        List<Integer> yAxis = new ArrayList<>();
        SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM");
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        //当前时间减13个月
        calendar.add(Calendar.MONTH,-13);
        for (int i=1;i<=13;i++){
            calendar.add(Calendar.MONTH,1);
            //将date转为时间格式
            String dateFormat = formats.format(calendar.getTime());
            xAxis.add(dateFormat);
            //对时间进行拼接到每月最后一天
            dateFormat=dateFormat+"-31";
            QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
            //查询到每个月底的个数
            memberQueryWrapper.lt("regTime",dateFormat);
            Integer integer = memberMapper.selectCount(memberQueryWrapper);
            yAxis.add(integer);
        }
        MemberVo memberVo = new MemberVo(xAxis,yAxis);

        return memberVo;
    }
}
